<?php
namespace Home\Model;
use Think\Model;

class CommonModel extends Model {

    public $err;
    //数据列表
    public function getList($table,$map,$field,$page,$num,$sort,$join = '')
    {
        $pages  =   getPage($table,$map,$page,$num,5,$join);
        $lists  =   M($table)->join($join,"left")->where($map)->limit($pages['page']*$num,$num)
            ->field($field)->order($sort)
            ->select();

        $info['lists']  =   $lists;
        $info['pages']  =   $pages;
        return $info;
    }


    //数据信息
    public function getInfo($table,$map,$field,$join = '')
    {
        return M($table)->join($join)->where($map)->field($field)->find();
    }

    public function addDo($table,$data,$file='')
    {

        if($file){
            $dir="./Uploads/{$table}/";
            if(!is_dir($dir)) {
                mkdir($dir, 0777, true);
            }

            if($file['name']) {
                $getP = upPic($file, $dir);
                if ($getP['tag'] == '1') {
                    $this->err = $getP['msg'];
                    return false;
                } else {
                    $opic = $this->where(array('id' => $data['id']))->getField("pic");
                    $data['pic'] = $getP;
                }
            }
        }

        if($data['id']){ // 编辑
            $res    =   M($table)->where(array("id"=>$data['id']))->save($data);

        }else{ // 添加
            $data['add_time']   =   getTime();
            $res    =   M($table)->add($data);
        }

        if($res === false){
            $this->err  =   "操作失败";
            delPic($data['pic']); return false;
        }else{
            delPic($opic);
            return true;
        }
    }

    //分类
    public function getCate($table)
    {
        $lists  =    M($table)->select();
        $lists  =   getTrees($lists);
        $lists  =   getOrderTrees($lists);
        return $lists;
    }

    public function getCateList($table)
    {
        $lists  =    M($table)->select();
        $lists  =   getTrees($lists);
        $lists  =   getOrderTreesList($lists);
        return $lists;
    }

    //分类id
    public function getCateIds($table,$cate_id)
    {

        if($cate_id === '') return '';
        else{

            $ids    =   M($table)->where(array("p_id"=>$cate_id))->field("id")->select();
            $str    =   $cate_id;
            foreach($ids as $val){
                $str    =   $str.','.$val['id'];
            }
            return $str;
        }
    }
    
    //获取菜单导航
    public function getCateNav($table)
    {
        $lists  =    M($table)->select();
        $lists  =   getTrees($lists);
        return $lists;
    }

    public function setView($table,$id)
    {
        M($table)->where(array('id'=>$id))->setInc('view');
    }

    //上下页
    public function upDown($table,$sql,$id)
    {
        $sql["_string"] =   "id < {$id}";
        $info['up']     =   M($table)->where($sql)->find();
        $sql["_string"] =   "id > {$id}";
        $info['down']   =   M($table)->where($sql)->find();
        return $info;
    }



}